package com.ruoyi.riskAssessment.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.riskAssessment.dto.RiskIdentifyEvaluationDto;
import com.ruoyi.riskAssessment.vo.RiskIdentifyEvaluationVo;
import com.ruoyi.system.domain.SysDictItem;
import com.ruoyi.system.mapper.SysDictItemMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.riskAssessment.mapper.RiskIdentifyEvaluationMapper;
import com.ruoyi.riskAssessment.domain.RiskIdentifyEvaluation;
import com.ruoyi.riskAssessment.service.IRiskIdentifyEvaluationService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 危险源-识别评估Service业务层处理
 *
 * @author lth
 * @date 2023-12-20
 */
@Service
public class RiskIdentifyEvaluationServiceImpl implements IRiskIdentifyEvaluationService {
    @Autowired
    private RiskIdentifyEvaluationMapper riskIdentifyEvaluationMapper;

    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    /**
     * 查询危险源-识别评估
     *
     * @param riskIdentifyEvaluationId 危险源-识别评估主键
     * @return 危险源-识别评估
     */
    @Override
    public RiskIdentifyEvaluation selectRiskIdentifyEvaluationByRiskIdentifyEvaluationId(String riskIdentifyEvaluationId) {
        return riskIdentifyEvaluationMapper.selectRiskIdentifyEvaluationByRiskIdentifyEvaluationId(riskIdentifyEvaluationId);
    }

    /**
     * 查询危险源-识别评估列表
     *
     * @param riskIdentifyEvaluation 危险源-识别评估
     * @return 危险源-识别评估
     */
    @Override
    public List<RiskIdentifyEvaluation> selectRiskIdentifyEvaluationList(RiskIdentifyEvaluation riskIdentifyEvaluation) {
        return riskIdentifyEvaluationMapper.selectRiskIdentifyEvaluationList(riskIdentifyEvaluation);
    }

    /**
     * 新增危险源-识别评估
     *
     * @param riskIdentifyEvaluation 危险源-识别评估
     * @return 结果
     */
    @Override
    public int insertRiskIdentifyEvaluation(RiskIdentifyEvaluation riskIdentifyEvaluation) {
        riskIdentifyEvaluation.setCreateTime(DateUtils.getNowDate());
        return riskIdentifyEvaluationMapper.insertRiskIdentifyEvaluation(riskIdentifyEvaluation);
    }

    /**
     * 修改危险源-识别评估
     *
     * @param riskIdentifyEvaluation 危险源-识别评估
     * @return 结果
     */
    @Override
    public int updateRiskIdentifyEvaluation(RiskIdentifyEvaluation riskIdentifyEvaluation) {
        return riskIdentifyEvaluationMapper.updateRiskIdentifyEvaluation(riskIdentifyEvaluation);
    }

    /**
     * 批量删除危险源-识别评估
     *
     * @param riskIdentifyEvaluationIds 需要删除的危险源-识别评估主键
     * @return 结果
     */
    @Override
    public int deleteRiskIdentifyEvaluationByRiskIdentifyEvaluationIds(String[] riskIdentifyEvaluationIds) {
        return riskIdentifyEvaluationMapper.deleteRiskIdentifyEvaluationByRiskIdentifyEvaluationIds(riskIdentifyEvaluationIds);
    }

    /**
     * 删除危险源-识别评估信息
     *
     * @param riskIdentifyEvaluationId 危险源-识别评估主键
     * @return 结果
     */
    @Override
    public int deleteRiskIdentifyEvaluationByRiskIdentifyEvaluationId(String riskIdentifyEvaluationId) {
        return riskIdentifyEvaluationMapper.deleteRiskIdentifyEvaluationByRiskIdentifyEvaluationId(riskIdentifyEvaluationId);
    }

    /**
     * 待上报危险源列表
     *
     * @return
     * @Author lth
     * @Description //TODO
     * @Date 13:58 2024/1/4
     * @Param
     **/
    @Override
    public PageInfo<RiskIdentifyEvaluationDto> riskWaitReportList(RiskIdentifyEvaluationVo vo) {
        String reportStatus = vo.getReportStatus();
        String assessmentLevel = vo.getAssessmentLevel();
        List<RiskIdentifyEvaluation> riskIdentifyEvaluationList = riskIdentifyEvaluationMapper.getRiskIdentifyEvaluationListByReportStatus(reportStatus, assessmentLevel);

        PageInfo Info = new PageInfo<>(riskIdentifyEvaluationList);

        List<RiskIdentifyEvaluationDto> dtoList = riskIdentifyEvaluationList.stream().map(riskIdentifyEvaluation -> {
            RiskIdentifyEvaluationDto riskIdentifyEvaluationDto = new RiskIdentifyEvaluationDto();
            BeanUtils.copyProperties(riskIdentifyEvaluation, riskIdentifyEvaluationDto);

            String riskSourceTypeId = riskIdentifyEvaluation.getRiskSourceTypeId();
            if (riskSourceTypeId != null) {
                SysDictItem riskType = sysDictItemMapper.getRiskType(riskIdentifyEvaluation.getRiskSourceTypeId());
                if (riskType!=null){
                    riskIdentifyEvaluationDto.setRiskSourceType(riskType.getItemText());
                }
            }
            String riskStatusId = riskIdentifyEvaluation.getRiskStatusId();
            if (riskStatusId != null) {
                SysDictItem statusByStatusId = sysDictItemMapper.getStatusByStatusId(riskIdentifyEvaluation.getRiskStatusId());
                if (statusByStatusId!=null){
                    riskIdentifyEvaluationDto.setRiskStatus(statusByStatusId.getItemText());
                }
            }
            String professionalTypeId = riskIdentifyEvaluation.getProfessionalTypeId();
            if (professionalTypeId != null) {
                SysDictItem professional = sysDictItemMapper.getProfessional(riskIdentifyEvaluation.getProfessionalTypeId());
                if (professional!=null){
                    riskIdentifyEvaluationDto.setProfessionalType(professional.getItemText());
                }
            }
            String scheduleTaskId = riskIdentifyEvaluation.getScheduleTaskId();
            if (scheduleTaskId != null) {
                SysDictItem scheduleTaskByScheduleTaskId = sysDictItemMapper.getScheduleTaskByScheduleTaskId(riskIdentifyEvaluation.getScheduleTaskId());
                if (scheduleTaskByScheduleTaskId!=null){
                    riskIdentifyEvaluationDto.setScheduleTask(scheduleTaskByScheduleTaskId.getItemText());
                }
            }

            riskIdentifyEvaluationDto.setRiskIdentifyEvaluationId(riskIdentifyEvaluation.getRiskIdentifyEvaluationId());
            return riskIdentifyEvaluationDto;
        }).collect(Collectors.toList());
        Info.setList(dtoList);

        return Info;
    }
}
